<?php

/**
 * This is the model class for table "bills".
 *
 * The followings are the available columns in table 'bills':
 * @property integer $id
 * @property string $name
 * @property integer $supplierid
 * @property double $sumtopay
 * @property string $timeline
 * @property string $created
 * @property string $description
 * @property double $paid
 *
 * The followings are the available model relations:
 * @property Billsoutcome[] $billoutcomes
 * @property Suppliers $supplier
 * @property Store[] $stores
 */
class Bills extends CActiveRecord
{	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Bills the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function init()
	{
		$this->created = Yii::app()->session->get("currentdate",date('Y-m-d'));
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bills';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('created', 'required'),
			array('supplierid', 'numerical', 'integerOnly'=>true),
			array('sumtopay, paid', 'numerical'),
			array('name, timeline, description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, supplierid, sumtopay, timeline, created, description, paid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'billoutcomes' => array(self::HAS_MANY, 'Billsoutcome', 'billid'),
			'supplier' => array(self::BELONGS_TO, 'Suppliers', 'supplierid'),
			'stores' => array(self::HAS_MANY, 'Store', 'billid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'supplierid' => 'Supplierid',
			'sumtopay' => 'Sumtopay',
			'timeline' => 'Timeline',
			'created' => 'Created',
			'description' => 'Description',
			'paid' => 'Paid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria(array('with'=>array('billoutcomes'),'order'=>'id DESC'));

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		if(isset($this->supplierid)&&$this->supplierid<=0) $this->supplierid="";
		$criteria->compare('supplierid',$this->supplierid);
		$criteria->compare('sumtopay',$this->sumtopay);
		$criteria->compare('timeline',$this->timeline,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('description',$this->description,true);		
		if(isset($this->paid)&&$this->paid<0) $this->paid="";
		$criteria->compare('paid',$this->paid);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public static function getPaidStatusText($val="")
	{
		if($val=="0") return "Не оплачено";
		else if($val=="1") return "Оплачено";
		else return "Не встановлено";
	}
	
	public function getPaidSum()
	{
		$sum = 0;
		//var_dump($this->billoutcomes);
		if(isset($this->billoutcomes))
		{
			foreach($this->billoutcomes as $bo)
			{
				$sum+=$bo->sum;
			}
		}
		return $sum;
	}
}